﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using MToolStudio.Render;
using Microsoft.Mtool.FSM;

namespace MToolStudio.UI
{
    public class DefaultActionTool : UITool
    {
        private IRenderable currentMoving;
        private bool bPan;
        private Point panLastMousePos;
        private Point offset;

        public DefaultActionTool(FSMDesigner designer)
            : base(designer)
        { }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            e = TransformToLocal(e);
            if (e.Button == MouseButtons.Left)
            {
                foreach (IRenderable r in Designer.RenderItems)
                {
                    if (r.IsSelected(new Point(e.X, e.Y)))
                    {
                        Designer.FocusedObject = r;
                        currentMoving = r;
                        Designer.Refresh();
                        offset = new Point(r.Position.X - e.X, r.Position.Y - e.Y);
                        break;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                bPan = true;
                panLastMousePos = new Point(e.X, e.Y);
            }
        }

        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (currentMoving != null)
                {
                    e = TransformToLocal(e);
                    currentMoving.Position = new Point(e.X + offset.X, e.Y + offset.Y);
                    Designer.Refresh();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (bPan)
                {
                    e = TransformToLocal(e);
                    int dx = e.X - panLastMousePos.X;
                    int dy = e.Y - panLastMousePos.Y;
                    //if (dx * dx + dy * dy > 4)//avoid the mouse shivering...
                    //{
                        Designer.RenderTranslation = new Point(Designer.RenderTranslation.X + dx,
                            Designer.RenderTranslation.Y + dy);
                        panLastMousePos = new Point(e.X, e.Y);
                        //Designer.Refresh();//TODO: cannot avoid the mouse shivering
                    //}
                }
            }
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMoving = null;
            }
            else if (e.Button == MouseButtons.Right)
            {
                bPan = false;
                Designer.Refresh();
            }

        }

        public override void OnKeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public override void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
                Designer.DeleteObject(Designer.FocusedObject);
            else
                base.OnKeyDown(sender, e);
        }

        public override void OnMouseScroll(object sender, MouseEventArgs e)
        {            
            Designer.RenderScale += e.Delta * 0.001f;
            Designer.Refresh();
        }
    }
}